package com.ramblingcoder.bc2.server.tool.update;

import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.ramblingcoder.bc2.server.tool.update.R;
import com.ramblingcoder.bc2.server.tool.update.commands.SupportingFunctions;
import com.ramblingcoder.bc2.server.tool.update.exceptions.InvalidArguments;
import com.ramblingcoder.bc2.server.tool.update.exceptions.LogInRequired;
import com.ramblingcoder.bc2.server.tool.update.exceptions.PlayerAlreadyInList;
import com.ramblingcoder.bc2.server.tool.update.exceptions.PlayerNotInList;
import com.ramblingcoder.bc2.server.tool.update.objects.InfoShare;
import com.ramblingcoder.bc2.server.tool.update.objects.Reserved;

public class ReservedView extends Activity implements OnClickListener, OnItemClickListener, OnFocusChangeListener {
	//Variables-----\
	private Button add;
	private ListView nameList;
	private EditText name;
	
	private Reserved[] reservedList;
	//Variables-----/
	
	//Initializer-----\
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.reservedview);

		nameList = (ListView) findViewById(R.id.namelist);
        add = (Button) findViewById(R.id.add);
        name = (EditText) findViewById(R.id.name);
        
        try {
			populate();
		} catch (IOException e) {
			SupportingFunctions.showError(this, e.getMessage(), new Intent(this, BC2ServerChoice.class));
		} catch (InvalidArguments e) {
			SupportingFunctions.showError(this, e.getMessage(), new Intent(this, BC2ServerChoice.class));
		} catch (LogInRequired e) {
			SupportingFunctions.showError(this, e.getMessage(), new Intent(this, BC2ServerChoice.class));
		}
		
        name.setOnFocusChangeListener(this);
		add.setOnClickListener(this);
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
	    menu.add(1, 5, 0, "Main Menu");
	    menu.add(1, 0, 0, "Refresh");
	    menu.add(1, 1, 0, "Players View");
	    menu.add(1, 2, 0, "Bans View");
	    menu.add(1, 3, 0, "Settings View");
	    menu.add(1, 4, 0, "Chat View");
	    menu.add(1, 6, 0, "Map View");
	    return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	    case 5:	
	    	finish();
	    	startActivity(new Intent(this, BC2ServerChoice.class));
	    	break;
	    case 0:
	    	finish();
	    	startActivity(new Intent(this, ReservedView.class));
	    	break;
	    case 1:	
	    	finish();
	    	startActivity(new Intent(this, PlayerView.class));
	    	break;
	    case 2:	
	    	finish();
	    	startActivity(new Intent(this, BanView.class));
	    	break;
	    case 3:
	    	finish();
	    	startActivity(new Intent(this, SettingsView.class));
	    	break;
	    case 4:
	    	finish();
	    	startActivity(new Intent(this, ChatView.class));
	    	break;
	    case 6:
	    	finish();
	    	startActivity(new Intent(this, MapView.class));
	    	break;
	    }
	    return true;
	}

	private void populate() throws IOException, InvalidArguments, LogInRequired {	
		reservedList = Reserved.listReserved(InfoShare.getCommand());
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
		nameList.setAdapter(adapter);
		
		for (Reserved reserved : reservedList)
			adapter.add(reserved.getName());
		
		nameList.setOnItemClickListener(this);
	}
	//Processes-----/
	
	//Events-----\
	public void onClick(View v) {
		if (v.getId() == R.id.add){
			try {
				if (Reserved.add(InfoShare.getCommand(), name.getText().toString()))
					SupportingFunctions.showMessage(this, "Success.", "Reserve slot successfuly added.", new Intent(this, ReservedView.class));
			} catch (IOException e) {
				SupportingFunctions.showError(this, e.getMessage(), new Intent(this, BC2ServerChoice.class));
			} catch (InvalidArguments e) {
				SupportingFunctions.showError(this, e.getMessage());
			} catch (LogInRequired e) {
				SupportingFunctions.showError(this, e.getMessage(), new Intent(this, BC2ServerChoice.class));
			} catch (PlayerAlreadyInList e) {
				SupportingFunctions.showError(this, e.getMessage());
			}
		}
	}
	
	public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		
		builder.setTitle("Remove Reserved");
		builder.setMessage("Remove " + reservedList[position].getName()+"?");
		
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				try {
					if (Reserved.remove(InfoShare.getCommand(), reservedList[position]))
						SupportingFunctions.showMessage(ReservedView.this, "Success.", "Reserve slot successfuly removed.", new Intent(ReservedView.this, ReservedView.class));
				} catch (IOException e) {
					SupportingFunctions.showError(ReservedView.this, e.getMessage(), new Intent(ReservedView.this, BC2ServerChoice.class));
				} catch (PlayerNotInList e) {
					SupportingFunctions.showError(ReservedView.this, e.getMessage());
				} catch (LogInRequired e) {
					SupportingFunctions.showError(ReservedView.this, e.getMessage(), new Intent(ReservedView.this, BC2ServerChoice.class));
				} catch (InvalidArguments e) {
					SupportingFunctions.showError(ReservedView.this, e.getMessage());
				}
			}
		}); 
        
        builder.setNegativeButton("Cancel", null); 
		
        builder.show();
	}

	boolean cleared = false;
	public void onFocusChange(View v, boolean hasFocus) {
		if (cleared == false){
			name.setText("");
			cleared = true;
		}
	}
    
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        	finish();
        	startActivity(new Intent(this, PlayerView.class));
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }
	//Events-----/
}
